package si.assign1.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="product_types")
@NamedQueries({
    @NamedQuery(name="listProductTypes",
                query="SELECT pt FROM ProductType pt"),
    @NamedQuery(name="getProductTypesById",
                query="SELECT pt FROM ProductType pt WHERE pt.id = :id"),
    @NamedQuery(name="getProductTypesByName",
                query="SELECT pt FROM ProductType pt WHERE pt.name = :name")
})
public class ProductType 
{
	public ProductType(){}

	/*
	 * Properties
	 */
	
	/*
	 * Set and get id
	 */
	public int getId()
	{   return id;    }
	public void setId(int id)
	{   this.id = id; }
	
	/*
	 * Set and get name
	 */
	public String getName()
	{   return name;   }
	public void setName(String name)
	{   this.name = name; }
	
	/**
	 * @return categories
	 */
	public Set<Category> getCategories() {
		return this.categories;
	}
	
	/**
	 * @param categories categories to set
	 */
	public void setCategories(Set<Category> categories) {
		this.categories = categories;
	}
	
	/**
	 * @param category category to add
	 */
	public void addCategory(Category category) {
		this.categories.add(category);
		category.setProductType(this);
	}
	
	/**
	 * @param category category to remove
	 */
	public void removeCategory(Category category) {
		this.categories.remove(category);
		category.setProductType(null);
	}
	
	/**
	 * @return products
	 */
	public Set<Product> getProducts() {
		return this.products;
	}
	
	/**
	 * @param products products to set
	 */
	public void setProducts(Set<Product> products) {
		this.products = products;
	}
	
	/**
	 * @param product product to add
	 */
	public void addProduct(Product product) {
		this.products.add(product);
		product.setProductType(this);
	}
	
	/**
	 * @param product product to remove
	 */
	public void removeProduct(Product product) {
		this.products.remove(product);
		product.setProductType(null);
	}
	
	/**
	 * @return product type attributes
	 */
	public Set<ProductTypeAttribute> getProductTypeAttributes() {
		return this.productTypeAttributes;
	}
	
	/**
	 * @param productTypeAttributes set of attributes to set
	 */
	public void setProductTypeAttributes(Set<ProductTypeAttribute> productTypeAttributes) {
		this.productTypeAttributes = productTypeAttributes;
	}
	
	/**
	 * @param productTypeAttribute the attribute to add
	 */
	public void addProductTypeAttribute(ProductTypeAttribute productTypeAttribute) {
		this.productTypeAttributes.add(productTypeAttribute);
		productTypeAttribute.setProductType(this);
	}

	/**
	 * @param productTypeAttribute the attribute to remove
	 */
	public void removeProductTypeAttribute(ProductTypeAttribute productTypeAttribute) {
		this.productTypeAttributes.remove(productTypeAttribute);
		productTypeAttribute.setProductType(null);
	}

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Basic(optional=false)
	@Column(name="id", nullable=true)
	private int id;
	
	@Column(name="name", length=255, nullable=false)
	private String name;
	
	/*
	 * One-to-many mapping from ProductType to Product 
	 * which contains all products of this product type 
	 */
	@OneToMany(mappedBy="productType")
	private Set<Product> products = new HashSet<Product>();
	
	/*
	 * One-to-many mapping from ProductType to ProductTypeAttribute
	 * which contains all attributes this product type possesses
	 */
	@OneToMany(mappedBy="productType")
	private Set<ProductTypeAttribute> productTypeAttributes
		= new HashSet<ProductTypeAttribute>();
	
	/*
	 * One-to-many mapping from ProductType to Category
	 */
	@OneToMany(mappedBy="productType")
	private Set<Category> categories = new HashSet<Category>();
}
